﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using ARS.PublicTool.WebHelper;
//using MySql.Data.MySqlClient;

namespace ARS.DataBase.Provider
{
    public class DbFactory
    {
        private static string dbTypeStr = ARS.PublicTool.WebHelper.ConfigHelper.GetConfigValueByKey("DbType").ToUpper();
        public static DbType dbType = DbTypeEnumParse<DbType>(dbTypeStr);

        //不使用缓存
        private static object CreateObjectNoCache(string AssemblyPath, string classNamespace)
        {
            try
            {
                object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                return objType;
            }
            catch//(System.Exception ex)
            {
                //string str=ex.Message;// 记录错误日志
                return null;
            }

        }
        //使用缓存
        private static object CreateObject(string AssemblyPath, string classNamespace)
        {
            object objType = DataCache.GetCache(classNamespace);
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    DataCache.SetCache(classNamespace, objType);// 写入缓存
                }
                catch//(System.Exception ex)
                {
                    //string str=ex.Message;// 记录错误日志
                }
            }
            return objType;
        }

        public static string GetDbTypeStr()
        {
            switch (dbType)
            {
                case DbType.MYSQL:
                    return "MySqlProvider";
                    break;
                case DbType.SQLSERVER:
                    return "SqlServerProvider";
                    break;
            }
            throw new Exception("不支持的数据库");
        }

        //private static string dbTypeStr;
        //public static string GetDbType()
        //{
        //    if (dbTypeStr == null)
        //    {
        //        dbTypeStr = ConfigurationManager.AppSettings["DbType"] ?? "SqlServerProvider";

        //        dbType = DbTypeEnumParse<DbType>(dbTypeStr);
        //    }
            
            
        //    return dbTypeStr;
        //    //string ClassNamespace = AssemblyPath + ".bai_bank";
        //    //object objType = CreateObject(AssemblyPath, ClassNamespace);
        //    //return (Bank.Basics.IDAL.Ibai_bank)objType;
        //}


        /// 根据配置文件中所配置的数据库类型
        /// 和传入的参数来创建相应数据库的参数数组对象
        /// </summary>
        /// <returns></returns>
        public static IDbDataParameter[] CreateDbParameters(int size)
        {
            int i = 0;
            IDbDataParameter[] param = null;
            switch (dbType)
            {
                case DbType.SQLSERVER:
                    param = new SqlParameter[size];
                    while (i < size) { param[i] = new SqlParameter(); i++; }
                    break;
                case DbType.MYSQL:
                    try
                    {
                        param = new MySql.Data.MySqlClient.MySqlParameter[size];
                        while (i < size)
                        {
                            param[i] = new MySql.Data.MySqlClient.MySqlParameter();
                            i++;
                        }
                    }
                    catch
                    {

                    }
                    break;
                default:
                    throw new Exception("数据库类型目前不支持！");

            }

            return param;
        }


        private static bool DbTypeIs(DbType type)
        {
            if (dbType == type)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是不是sql数据库
        /// </summary>
        /// <returns></returns>
        public static bool DbTypeIsSqlServer()
        {
            return DbTypeIs(DbType.SQLSERVER);
        }
        /// <summary>
        /// 是不是mysql
        /// </summary>
        /// <returns></returns>
        public static bool DbTypeIsMySql()
        {
            return DbTypeIs(DbType.MYSQL);
        }

        /// <summary>
        /// 用于数据库类型的字符串枚举转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DbTypeEnumParse<T>(string value)
        {
            try
            {
                return (T)Enum.Parse(typeof(T), value);
            }
            catch
            {
                throw new Exception("数据库类型\"" + value + "\"错误，请检查！");
            }
        }

        private static IDataPagingProvider _dpProvider;
        /// <summary>
        /// DataPaging数据操作
        /// </summary>
        public static IDataPagingProvider DpProvider
        {
            get
            {
                if (_dpProvider == null)
                {
                    _dpProvider = ARS.DataBase.Provider.DataPagingProvider.GetInstance();
                }
                return _dpProvider;
            }
        }

        private static IDbProvider _dbProvider;
        /// <summary>
        /// DbProvider
        /// </summary>
        public static IDbProvider DbProvider
        {
            get
            {
                if (_dbProvider == null)
                {
                    _dbProvider = ARS.DataBase.Provider.DbProvider.GetInstance();
                }
                return _dbProvider;
            }
        }

        private static DataContext _dataContext;
        /// <summary>
        /// linq用的
        /// </summary>
        public static DataContext DataContext
        {
            get
            {
                if (_dataContext == null)
                {
                    _dataContext = new DataContext(PubConstant.GetConnectionString(PubConstant.connectionString));
                }
                return _dataContext;
            }
        }
    }
}
